package com.flashdeck.activity;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.flashdeck.Extras;
import com.flashdeck.R;
import com.flashdeck.dao.DeckDao;
import com.flashdeck.dao.SettingsDao;
import com.flashdeck.dao.impl.DeckDaoImpl;
import com.flashdeck.dao.impl.SettingsDaoImpl;
import com.flashdeck.domain.DeckInfo;

public class SelectDeckActivity extends ListActivity {

	private final int NO_DECKS_AVAILABLE_DIALOG = 1;
	
	private final DeckDao deckDao;
	
	private final SettingsDao settingsDao; 
	
	public SelectDeckActivity() {
		deckDao = new DeckDaoImpl(this);
		settingsDao = new SettingsDaoImpl(this);
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		
		List<DeckInfo> decks = getDeckDao().listDeckInfos();

		if (decks.isEmpty()) {
			/*
			 * No decks available, show warning message and take user to
			 * DownloadDecks activity or specify decks location in
			 * SettingsActivity
			 */
			showDialog(NO_DECKS_AVAILABLE_DIALOG);			
		} else {
			setListAdapter(new ArrayAdapter<DeckInfo>(this, android.R.layout.simple_list_item_1, decks));
			getListView().setTextFilterEnabled(true);	
		}
				
		registerForContextMenu(getListView());		
	}
	

	/* (non-Javadoc)
	 * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		DeckInfo deckInfo = (DeckInfo) getListAdapter().getItem(position);

		Intent intent = new Intent(this, PlayDeckActivity.class);
		intent.putExtra(Extras.DECK_TO_PLAY, deckInfo.getId());
		
		startActivity(intent);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {		
		menu.
			add(R.string.get_decks_menu_item).
			setIntent(new Intent(this, DownloadDecksActivity.class));
		
		menu.
			add(R.string.settings_menu_item).
			setIntent(new Intent(this, SettingsActivity.class));
		
		menu.
			add(R.string.feedback_menu_item).
			setIntent(new Intent(this, FeedbackActivity.class));	
		
		return true;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {		
		super.onCreateContextMenu(menu, v, menuInfo);
		
		menu.
			add(R.string.context_details_item).
			setIntent(new Intent(this, DeckDetailsActivity.class));
		
		menu.
			add(R.string.context_remove_item).
			setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
				
				@Override
				public boolean onMenuItemClick(MenuItem item) {
					// TODO add confirmation dialog for removal
					AdapterContextMenuInfo cmi = (AdapterContextMenuInfo) item.getMenuInfo();
					
					DeckInfo deckInfo = (DeckInfo) SelectDeckActivity.this.getListAdapter().getItem(cmi.position);
					getDeckDao().removeDeck(deckInfo);					
					
					return true;
				}
			});
	}	

	/**
	 * @return the deckDao
	 */
	public DeckDao getDeckDao() {
		return deckDao;
	}

	/**
	 * @return the settingsDao
	 */
	public SettingsDao getSettingsDao() {
		return settingsDao;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateDialog(int)
	 */
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog result;
		switch (id) {
		case NO_DECKS_AVAILABLE_DIALOG: {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.no_decks_dialog_title);
			builder.setItems(R.array.no_decks_dialog_options, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					switch (which) {
					case 0: {
						/*
						 * Download new decks
						 */
						Intent intent = new Intent(SelectDeckActivity.this, DownloadDecksActivity.class);
						startActivity(intent);
						
						break;
					}
					case 1: {
						/*
						 * Locate decks
						 */
						Intent intent = new Intent(SelectDeckActivity.this, DownloadDecksActivity.class);
						startActivity(intent);

						break;
					}
					default: {
						/*
						 * TODO: Should show warning. No such option available
						 */
						break;
					}
					}
				}
			});
			
			result = builder.create();			
			break;
		}
		default: {
			result = super.onCreateDialog(id);
			break;
		}
		}		
		
		return result;
	}	
}